function taskPage()
{
	let userId = getCookie("userId") == "" ? "0" : getCookie("userId");
	let txt = "";
	$.ajax({
		type:"post",
		url:baseUrl + "/taskPage",
		dataType:"json",
		data:
		{
			"userId" : userId
		},
		async:true,
		success:function(data)
		{
			//针对未登录
			if(data["data"]["user"] == undefined)
			{
				if(data["data"]["list"].length == 0)
				{
					txt += `
						<div class="container">
							<table class=" table table-bordered table-striped table-hover text-center">
								<thead>
									<tr>
										<th class="text-center">序号</th>
										<th class="text-center">标题</th>
										<th class="text-center">老师</th>
										<th class="text-center">操作</th>
									</tr>
								</thead>
								<tbody>
									<tr>
										<td colspan="5">无数据</td>
									</tr>
								</tbody>
							</table>
						</div>
					`;
				}
				else 
				{
					txt += `
						<div class="container">
							<table class=" table table-bordered table-striped table-hover text-center">
								<thead>
									<tr>
										<th class="text-center">序号</th>
										<th class="text-center">标题</th>
										<th class="text-center">老师</th>
										<th class="text-center">操作</th>
									</tr>
								</thead>
								<tbody>`;
								for(let i = 0; i < data["data"]["list"].length; i++)
								{
									txt += `
										<tr>
											<td>` + (i + 1) + `</td>
											<td>` + data["data"]["list"][i]["taskTitle"] + `</td>
											<td>` + data["data"]["list"][i]["username"] + `</td>
											<td><a href="index.html?title=taskShow&taskId=` + data["data"]["list"][i]["taskId"] + `"><button class="btn btn-primary btn-xs">查看</button></a></td>
										</tr>
									`;
								}
								txt += `
								</tbody>
							</table>
						</div>
					`;
				}
			}
			//针对学生
			else if(data["data"]["user"]["type"] == 1)
			{
				if(data["data"]["list"].length == 0)
				{
					txt += `
						<div class="container">
							<table class=" table table-bordered table-striped table-hover text-center">
								<thead>
									<tr>
										<th class="text-center">序号</th>
										<th class="text-center">标题</th>
										<th class="text-center">老师</th>
										<th class="text-center">状态</th>
										<th class="text-center">操作</th>
									</tr>
								</thead>
								<tbody>
									<tr>
										<td colspan="5">无数据</td>
									</tr>
								</tbody>
							</table>
						</div>
					`;
				}
				else 
				{
					txt += `
						<div class="container">
							<table class=" table table-bordered table-striped table-hover text-center">
								<thead>
									<tr>
										<th class="text-center">序号</th>
										<th class="text-center">标题</th>
										<th class="text-center">老师</th>
										<th class="text-center">状态</th>
										<th class="text-center">操作</th>
									</tr>
								</thead>
								<tbody>`;
								for(let i = 0; i < data["data"]["list"].length; i++)
								{
									txt += `
										<tr>
											<td>` + (i + 1) + `</td>
											<td>` + data["data"]["list"][i]["taskTitle"] + `</td>
											<td>` + data["data"]["list"][i]["username"] + `</td>
											<td>` + (data["data"]["list"][i]["gradeNum"] == undefined ? "未提交" : (data["data"]["list"][i]["gradeNum"] == -1 ? "待批改" : "已批改")) + `</td>
											<td><a href="index.html?title=taskShow&taskId=` + data["data"]["list"][i]["taskId"] + `"><button class="btn btn-primary btn-xs">查看</button></a></td>
										</tr>
									`;
								}
								txt += `
								</tbody>
							</table>
						</div>
					`;
				}
			}
			//针对老师
			else if(data["data"]["user"]["type"] == 2)
			{
				//如果没有数据
				if(data["data"]["list"].length == 0)
				{
					txt += `
						<div class="container">
							<table class=" table table-bordered table-striped table-hover text-center">
								<thead>
									<tr >
										<th class="text-center">序号</th>
										<th class="text-center">标题</th>
										<th class="text-center">操作</th>
									</tr>
								</thead>
								<tbody>
									<tr>
										<td colspan="3">无数据</td>
									</tr>
								</tbody>
							</table>
						</div>
					`;
				}
				else 
				{
					txt += `
						<div class="container">
							<table class=" table table-bordered table-striped table-hover text-center">
								<thead>
									<tr >
										<th class="text-center">序号</th>
										<th class="text-center">标题</th>
										<th class="text-center">操作</th>
									</tr>
								</thead>
								<tbody>`;
								for(let i = 0; i < data["data"]["list"].length; i++)
								{
									txt += `
										<tr>
											<td>` + (i + 1) + `</td>
											<td>` + data["data"]["list"][i]["taskTitle"] + `</td>
											<td>
												<a href="index.html?title=taskShow&taskId=` + data["data"]["list"][i]["taskId"] + `"><button class="btn btn-primary btn-xs">查看</button></a>
												<button class="btn btn-danger btn-xs" onclick="deleteTask(` + data["data"]["list"][i]["taskId"] + `)">删除</button>
											</td>
										</tr>
									`;
								}
								txt += `
								</tbody>
							</table>
						</div>
					`;
					
				}
				
			}
			//针对管理员
			else if(data["data"]["user"]["type"] == 3)
			{
				
				if(data["data"]["list"].length == 0)
				{
					txt += `
						<div class="container">
							<table class=" table table-bordered table-striped table-hover text-center">
								<thead>
									<tr>
										<th class="text-center">序号</th>
										<th class="text-center">标题</th>
										<th class="text-center">老师</th>
										<th class="text-center">操作</th>
									</tr>
								</thead>
								<tbody>
									<tr>
										<td colspan="5">无数据</td>
									</tr>
								</tbody>
							</table>
						</div>
					`;
				}
				else 
				{
					txt += `
						<div class="container">
							<table class=" table table-bordered table-striped table-hover text-center">
								<thead>
									<tr>
										<th class="text-center">序号</th>
										<th class="text-center">标题</th>
										<th class="text-center">老师</th>
										<th class="text-center">操作</th>
									</tr>
								</thead>
								<tbody>`;
								for(let i = 0; i < data["data"]["list"].length; i++)
								{
									txt += `
										<tr>
											<td>` + (i + 1) + `</td>
											<td>` + data["data"]["list"][i]["taskTitle"] + `</td>
											<td>` + data["data"]["list"][i]["username"] + `</td>
											<td>
												<a href="index.html?title=taskShow&taskId=` + data["data"]["list"][i]["taskId"] + `"><button class="btn btn-primary btn-xs">查看</button></a>
												<button class="btn btn-danger btn-xs" onclick="deleteTask(` + data["data"]["list"][i]["taskId"] + `)">删除</button>
											</td>
										</tr>
									`;
								}
								txt += `
								</tbody>
							</table>
						</div>
					`;
				}
				
				
				
			}
			
			$("body").append(txt);
		},
		error:function()
		{
			txt += `
				<div class="container">
					<table class=" table table-bordered table-striped table-hover text-center">
						<thead>
							<tr>
								<th class="text-center">序号</th>
								<th class="text-center">标题</th>
								<th class="text-center">老师</th>
								<th class="text-center">状态</th>
								<th class="text-center">操作</th>
							</tr>
						</thead>
						<tbody>
							<tr>
								<td colspan="5">无数据</td>
							</tr>
						</tbody>
					</table>
				</div>
			`;
			$("body").append(txt);
		}
		
	});
	
	
}

function newTaskPage()
{
	let txt = "";
	txt += `
		<div class="container">
			<div class="form-group">
				<label for="taskTitle">标题</label>
				<input id="taskTitle" type="text" class="form-control"/>
			</div>
			<div class="form-group">
				<label for="taskMain">内容</label>
				<textarea id="taskMain" class="form-control" rows="8"></textarea>
			</div>
			<div><button class="btn btn-primary" style="margin: 20px;" onclick="addNewTask()">保存</button></div>
		</div>
	`;
	$("body").append(txt);
}

function addNewTask()
{
	let taskTitle = $("#taskTitle").val().trim();
	let taskMain = $("#taskMain").val().trim();
	if(taskTitle == "" || taskMain == "")
	{
		$("#myModalBody").text("请输入完整数据！");
		$("#myModal").modal("show");
		return;
	}
	$.ajax({
		type:"post",
		url:baseUrl + "/addNewTask",
		data:
		{
			"userId":getCookie("userId"),
			"taskTitle":taskTitle,
			"taskMain":taskMain
		},
		dataType:"json",
		success:function(data)
		{
			if(data["meta"]["status"] == "200")
			{
				window.location.assign("index.html?title=task");
			}
			else 
			{
				$("#myModalBody").text("增加失败！");
				$("#myModal").modal("show");
			}
		},
		error:function()
		{
			$("#myModalBody").text("网络错误！");
			$("#myModal").modal("show");
		}
		
	});
	
}


function taskShowPage()
{
	let txt = "";
	let userId = getCookie("userId") == "" ? "0" : getCookie("userId");
	$.ajax({
		type:"post",
		url:baseUrl + "/taskShowPage",
		dataType:"json",
		data:
		{
			"userId" : userId,
			"taskId" : analyseSearch("taskId")
		},
		async:true,
		success:function(data)
		{
//			console.log(data);
			type = data["data"]["user"]["type"];
			if(type == 1)
			{
				txt += `
					<div class="container">
						<h4>标题</h4>
						<div style="margin-left: 20px;">` + data["data"]["show"]["taskTitle"] + `</div>
						<h4>老师</h4>
						<div style="margin-left: 20px;">` + data["data"]["show"]["username"] + `</div>
						<h4>主要内容</h4>
						<div style="margin-left: 20px;">` + data["data"]["show"]["taskMain"] + `</div>
						<h4>提交状态</h4>
						<div style="margin-left: 20px;">` + (data["data"]["show"]["gradeNum"] == undefined ? "未提交" : (data["data"]["show"]["gradeNum"] == -1 ? "待批改" : "已批改")) + `</div>
						<h4>答案</h4>
				`;
				if(data["data"]["show"]["gradeNum"] == undefined)
				{
					txt += `
						<textarea id="gradeAnswer" class="form-control" rows="8"></textarea>
						<button class="btn btn-primary" style="margin: 20px;" onclick="addNewGrade()">提交</button>
					`;
				}
				else
				{
					txt += `
						<div style="margin-left: 20px;">` + data["data"]["show"]["gradeAnswer"] + `</div>
					`;
					if(data["data"]["show"]["gradeNum"] != -1)
					{
						txt += `
							<h4>得分</h4>
							<div style="margin-left: 20px;">` + data["data"]["show"]["gradeNum"] + `</div>
						`;
					}
				}
					txt += `
					<div></div>
					<button class="btn btn-primary" onclick="history.go(-1)">返回</button>
				</div>
				`;
				
			}
			else if(type == 2 || type == 3)
			{
				txt += `
					<div class="container">
						<div class="form-group">
							<label for="taskTitle">标题</label>
							<input id="taskTitle" type="text" class="form-control" value="` + data["data"]["show"]["taskTitle"] + `"/>
						</div>
						<div class="form-group">
							<label for="taskMain">内容</label>
							<textarea id="taskMain" class="form-control" rows="8">` + data["data"]["show"]["taskMain"] + `</textarea>
						</div>
						<div><button class="btn btn-primary" style="margin: 20px;" onclick="updateTask()">保存</button></div>
					</div>
				`;
				
			}
			
			$("body").append(txt);
		},
		error:function()
		{
			
			txt += `
				<div class="container">
					<h4>标题</h4>
					<div style="margin-left: 20px;">……</div>
					<h4>老师</h4>
					<div style="margin-left: 20px;">……</div>
					<h4>主要内容</h4>
					<div style="margin-left: 20px;">……</div>
					<h4>提交状态</h4>
					<div style="margin-left: 20px;">……</div>
					<h4>答案</h4>
					<textarea class="form-control" rows="8"></textarea>
					<button class="btn btn-primary" style="margin: 20px;">提交</button>
					<div></div>
					<button class="btn btn-primary" onclick="history.go(-1)">返回</button>
				</div>
			`;
			
			$("body").append(txt);
			
		}
	});
	
}

function updateTask()
{
	let taskTitle = $("#taskTitle").val().trim();
	let taskMain = $("#taskMain").val().trim();
	if(taskTitle == "" || taskMain == "")
	{
		$("#myModalBody").text("请输入完整数据！");
		$("#myModal").modal("show");
		return;
	}
	$.ajax({
		type:"post",
		url:baseUrl + "/updateTaskById",
		data:
		{
			"taskId":analyseSearch("taskId"),
			"taskTitle":taskTitle,
			"taskMain":taskMain
		},
		dataType:"json",
		success:function(data)
		{
			if(data["meta"]["status"] == "200")
			{
				window.location.assign("index.html?title=task");
			}
			else 
			{
				$("#myModalBody").text("修改失败！");
				$("#myModal").modal("show");
			}
		},
		error:function()
		{
			$("#myModalBody").text("网络错误！");
			$("#myModal").modal("show");
		}
		
	});
	
	
}

function deleteTask(taskId)
{
	$.ajax({
		type:"post",
		url:baseUrl + "/deleteTaskById",
		async:true,
		dataType:"json",
		data:
		{
			"taskId":taskId
		},
		success:function(data)
		{
			if(data["meta"]["status"] == 200)
			{
				window.location.reload();
			}
			else 
			{
				$("#myModalBody").text("删除失败！");
				$("#myModal").modal("show");
			}
			
		},
		error:function()
		{
			$("#myModalBody").text("删除失败！");
			$("#myModal").modal("show");
		}
	});
	
	
}
